package com.example.commandpattern;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.example.commandpattern.brush.CircleBrush;
import com.example.commandpattern.brush.IBrush;
import com.example.commandpattern.brush.NormalBrush;
import com.example.commandpattern.canvas.DrawCanvas;
import com.example.commandpattern.draw.DrawPath;

public class MainActivity extends AppCompatActivity {

    private Button btnRed;
    private Button btnGreen;
    private Button btnBlue;

    private Button circleBrush;
    private Button circleNormal;

    private Button btnUndo;
    private Button btnRedo;

    private DrawCanvas drawCanvas;
    private DrawPath mPath;
    private Paint mPaint;
    private IBrush mBrush;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        btnRed = findViewById(R.id.btn_red);
        btnGreen = findViewById(R.id.btn_green);
        btnBlue = findViewById(R.id.btn_blue);

        circleBrush = findViewById(R.id.btn_circle_brush);
        circleNormal = findViewById(R.id.btn_normal_brush);

        btnUndo = findViewById(R.id.btn_undo);
        btnRedo = findViewById(R.id.btn_redo);

        drawCanvas = findViewById(R.id.canvas);
        drawCanvas.setOnTouchListener(new DrowTouchListener());

        btnRed.setOnClickListener(onClickListener);
        btnGreen.setOnClickListener(onClickListener);
        btnBlue.setOnClickListener(onClickListener);
        circleBrush.setOnClickListener(onClickListener);
        circleNormal.setOnClickListener(onClickListener);
        btnUndo.setOnClickListener(onClickListener);
        btnRedo.setOnClickListener(onClickListener);

        btnUndo.setEnabled(false);
        btnRedo.setEnabled(false);
    }

    private void initData() {
        mPaint = new Paint();
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setStrokeWidth(3);

        mBrush = new NormalBrush();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.btn_red:
                    mPaint = new Paint();
                    mPaint.setStrokeWidth(3);
                    mPaint.setColor(0xFFFF0000);
                    break;
                case R.id.btn_green:
                    mPaint = new Paint();
                    mPaint.setStrokeWidth(3);
                    mPaint.setColor(0xFF00FF00);
                    break;
                case R.id.btn_blue:
                    mPaint = new Paint();
                    mPaint.setStrokeWidth(3);
                    mPaint.setColor(0xFF0000FF);
                    break;
                case R.id.btn_circle_brush:
                    mBrush = new CircleBrush();
                    break;
                case R.id.btn_normal_brush:
                    mBrush = new NormalBrush();
                    break;
                case R.id.btn_undo:
                    drawCanvas.undo();
                    if (!drawCanvas.canUndo()) {
                        btnUndo.setEnabled(false);
                    }
                    btnRedo.setEnabled(true);
                    break;
                case R.id.btn_redo:
                    drawCanvas.redo();
                    if (!drawCanvas.canRedo()) {
                        btnRedo.setEnabled(false);
                    }
                    btnUndo.setEnabled(true);
                    break;
            }
        }
    };

    private class DrowTouchListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mPath = new DrawPath();
                mPath.paint = mPaint;
                mPath.path = new Path();
                mPath.path.setFillType(Path.FillType.WINDING);
                mBrush.down(mPath.path, event.getX(), event.getY());
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                mBrush.move(mPath.path, event.getX(), event.getY());
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mBrush.up(mPath.path, event.getX(), event.getY());
                drawCanvas.add(mPath);
                drawCanvas.isDrawing = true;
                btnUndo.setEnabled(true);
                btnRedo.setEnabled(false);
            }
            return true;
        }
    }
}
